﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utilities;
using System.Windows.Forms;

namespace SM2
{
    public class Loader
    {

        public static void LoadREPOSITORY()
        {
            loadSM2_REPOSITORY();
            loadFolder();
            loadGlobalConfig();
            loadGlobalFile();
        }

        public static void loadSM2_REPOSITORY()
        {
            //Utilities.GlobalFolder.REPOSITORY = "D:\\Builds\\14jun\\SM2_REPOSITORY\\";
            Utilities.GlobalFolder.REPOSITORY = SM2.Properties.Settings.Default.SM2_REPOSITORY;
            if (Utilities.GlobalFolder.REPOSITORY.Equals(""))
            {
                FolderBrowserDialog fbDlg = new FolderBrowserDialog();
                if (fbDlg.ShowDialog() == DialogResult.OK)
                {
                    string folderName = fbDlg.SelectedPath;
                    Utilities.GlobalFolder.REPOSITORY = folderName + "\\";
                    SM2.Properties.Settings.Default.SM2_REPOSITORY = Utilities.GlobalFolder.REPOSITORY;
                    SM2.Properties.Settings.Default.Save();
                }
            }
            Utilities.GlobalObject.LOGGER.log("Loaded SM2_REPOSITORY folder = [" + Utilities.GlobalFolder.REPOSITORY +"]");
        }

        public static void loadFolder()
        {
            //Step 1. Set OTHER REPOSITORY folders
            Utilities.GlobalFolder.REPOSITORY_TESTSUITE = Utilities.GlobalFolder.REPOSITORY + "TESTSUITE\\";
            Utilities.GlobalFolder.REPOSITORY_LOG = Utilities.GlobalFolder.REPOSITORY + "LOG\\";
            Utilities.GlobalFolder.REPOSITORY_CONFIG = Utilities.GlobalFolder.REPOSITORY + "CONFIG\\";
            Utilities.GlobalFolder.REPOSITORY_TESTDATA = Utilities.GlobalFolder.REPOSITORY + "TESTDATA\\";
            Utilities.GlobalFolder.REPOSITORY_WEBAii_LOG = Utilities.GlobalFolder.REPOSITORY + "WEBAII_LOG\\";
            Utilities.GlobalFile.CONFIG_FILENAME = Utilities.GlobalFolder.REPOSITORY_CONFIG + "CONFIG.xml";
            Utilities.GlobalFolder.REPOSITORY_LOG_CURRENTLOG = FolderUtil.createFolder(Utilities.GlobalFolder.REPOSITORY_LOG, StringUtil.getNow_HH_MM_SS_DD_MMM_YYYY());
            Utilities.GlobalObject.LOGGER.log("Loaded REPOSITORY_TESTSUITE = [" + Utilities.GlobalFolder.REPOSITORY_TESTSUITE + "]");
            Utilities.GlobalObject.LOGGER.log("Loaded REPOSITORY_LOG = [" + Utilities.GlobalFolder.REPOSITORY_LOG + "]");
            Utilities.GlobalObject.LOGGER.log("Loaded REPOSITORY_CONFIG = [" + Utilities.GlobalFolder.REPOSITORY_CONFIG + "]");
            Utilities.GlobalObject.LOGGER.log("Loaded REPOSITORY_TESTDATA = [" + Utilities.GlobalFolder.REPOSITORY_TESTDATA + "]");
            Utilities.GlobalObject.LOGGER.log("Loaded REPOSITORY_WEBAii_LOG = [" + Utilities.GlobalFolder.REPOSITORY_WEBAii_LOG + "]");
            Utilities.GlobalObject.LOGGER.log("Loaded CONFIG_FILENAME= [" + Utilities.GlobalFile.CONFIG_FILENAME + "]");
            Utilities.GlobalObject.LOGGER.log("Loaded REPOSITORY_LOG_CURRENTLOG = [" + Utilities.GlobalFolder.REPOSITORY_LOG_CURRENTLOG + "]");
        }

        public static void loadGlobalConfig()
        {
            try
            {
                Utilities.GlobalInit.loadValueForGlobalConfig(Utilities.GlobalFile.CONFIG_FILENAME);
            }
            catch (Exception ex)
            {
                Utilities.GlobalObject.LOGGER.log("Failed to load Global Config in filename [" + Utilities.GlobalFile.CONFIG_FILENAME + "]");
                Utilities.GlobalObject.LOGGER.log(ex.ToString());
            }
        }

        public static void loadGlobalFile()
        {
            //Get name of filename
            try
            {
                Utilities.GlobalInit.loadValueForGlobalFile(Utilities.GlobalFile.CONFIG_FILENAME);
            }
            catch (Exception ex)
            {
                Utilities.GlobalObject.LOGGER.log("Failed to load Global File in filename [" + Utilities.GlobalFile.CONFIG_FILENAME + "]");
                Utilities.GlobalObject.LOGGER.log(ex.ToString());
            }

            //Set full path to filename
            Utilities.GlobalFile.REPORT_FILENAME = Utilities.GlobalFolder.REPOSITORY_LOG_CURRENTLOG + Utilities.GlobalFile.REPORT_FILENAME;
            Utilities.GlobalFile.LOG_FILENAME = Utilities.GlobalFolder.REPOSITORY_LOG_CURRENTLOG + Utilities.GlobalFile.LOG_FILENAME;
            Utilities.GlobalFile.MAPPING_FILENAME = Utilities.GlobalFolder.REPOSITORY_CONFIG + Utilities.GlobalFile.MAPPING_FILENAME;
            Utilities.GlobalFile.REPORT_TEMPLATE_FILENAME = Utilities.GlobalFolder.REPOSITORY_CONFIG + Utilities.GlobalFile.REPORT_TEMPLATE_FILENAME;
            Utilities.GlobalFile.SCHEDULER_FILENAME = Utilities.GlobalFolder.REPOSITORY_CONFIG + Utilities.GlobalFile.SCHEDULER_FILENAME;
            Utilities.GlobalFile.SERVERS_FILENAME = Utilities.GlobalFolder.REPOSITORY_CONFIG + Utilities.GlobalFile.SERVERS_FILENAME;
            Utilities.GlobalFile.SERVER_HARDATA_FILENAME = Utilities.GlobalFolder.REPOSITORY_CONFIG + Utilities.GlobalFile.SERVER_HARDATA_FILENAME;

            Utilities.GlobalObject.LOGGER.log("Loaded REPORT_FILENAME = [" + Utilities.GlobalFile.REPORT_FILENAME + "]");
            Utilities.GlobalObject.LOGGER.log("Loaded LOG_FILENAME = [" + Utilities.GlobalFile.LOG_FILENAME + "]");
            Utilities.GlobalObject.LOGGER.log("Loaded MAPPING_FILENAME = [" + Utilities.GlobalFile.MAPPING_FILENAME + "]");
            Utilities.GlobalObject.LOGGER.log("Loaded REPORT_TEMPLATE_FILENAME = [" + Utilities.GlobalFile.REPORT_TEMPLATE_FILENAME + "]");
            Utilities.GlobalObject.LOGGER.log("Loaded SCHEDULER_FILENAME = [" + Utilities.GlobalFile.SCHEDULER_FILENAME + "]");
            Utilities.GlobalObject.LOGGER.log("Loaded SERVERS_FILENAME = [" + Utilities.GlobalFile.SERVERS_FILENAME + "]");
            Utilities.GlobalObject.LOGGER.log("Loaded SERVER_HARDATA_FILENAME = [" + Utilities.GlobalFile.SERVER_HARDATA_FILENAME + "]");
        }
    }
}
